package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.CartItem;
import entity.ShoppingCart;

/**
 * Servlet implementation class ShoppingCartSubmitOrderServlet
 */
@WebServlet("/ShoppingCartComfirmOrder.html")
public class ShoppingCartComfirmOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] rateIds = request.getParameterValues("selectedRate");
		List<Integer> rateIdList = new ArrayList<Integer>();
		//把rateId转成Integer
		if(rateIds!=null&&rateIds.length>0){
			for (String string : rateIds) {
				rateIdList.add(Integer.valueOf(string));
			}
			//把选择的商品加入selectedOrderList
			ShoppingCart shoppingCartList = (ShoppingCart)request.getSession().getAttribute("shoppingCarList");
			List<CartItem> selectedOrderList = new ArrayList<CartItem>();
			for (CartItem cart : shoppingCartList.getCartItemList()) {
				for (Integer integer : rateIdList) {
					if(cart.getRate().getRateId().equals(integer)){
						selectedOrderList.add(cart);
					}
				}
			}
			
			//删除shoppingCarList里面选中的食物
			for (CartItem cartItem : selectedOrderList) {
				shoppingCartList.getCartItemList().remove(cartItem);
			}
			request.getSession().setAttribute("selectedOrderList", selectedOrderList);
			request.getRequestDispatcher("/makeSureOrder.jsp").forward(request, response);
			return;
		}
		response.getWriter().println("提交页面异常");
		response.getWriter().close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
